package com.university.sol.controller;

import com.university.sol.common.Constant;
import com.university.sol.common.ServerResponse;
import com.university.sol.common.UserStatus;
import com.university.sol.pojo.User;
import com.university.sol.service.UserService;
import com.university.sol.util.CookieUtil;
import com.university.sol.util.DataVerify;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 侯瑞阳
 * @Date 2019/6/7 0:20
 **/
@Controller
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;


    /**
     * 用户注册
     */
    @ResponseBody
    @PostMapping("user/register.html")
    public ServerResponse<User> userRegister(User user) {
        // 校验邮箱合法性
        if (StringUtils.isBlank(user.getUserEmail().trim()) && !DataVerify.verifyEmail(user.getUserEmail().trim())) {
            return ServerResponse.createByErrorMessage("请输入正确的邮箱号！");
        }
        // 检查密码合法性
        if (StringUtils.isBlank(user.getUserPassword().trim())) {
            return ServerResponse.createByErrorMessage("请输入合法的密码！");
        }
        // 检查邮箱是否被占用
        if(!userService.checkEmail(user.getUserEmail())) {
            return ServerResponse.createByErrorMessage("邮箱已经被占用！");
        }
        // 将信息写入数据库
        int result = userService.registerUser(user);
        if (result > 0) {
            logger.info("user {} register success", user.getUserEmail());
            return ServerResponse.createBySuccessMessage("注册成功！");
        }
        return ServerResponse.createByErrorMessage("注册失败！");
    }


    /**
     * 用户登录
     */
    @ResponseBody
    @PostMapping("user/login.html")
    public ServerResponse userLogin(User user, HttpServletRequest request) {
        //校验用户传递的邮箱是否合法
        if(StringUtils.isBlank(user.getUserEmail().trim()) && !DataVerify.verifyEmail(user.getUserEmail().trim())) {
            return ServerResponse.createByErrorMessage("请输入正确的邮箱号！");
        }
        // 去数据库查询邮箱和密码
        User loginUser = userService.getUserByEmailAndPassword(user.getUserEmail(), user.getUserPassword());
        // 判断查出来的用户是否为空
        if (null == loginUser) {
            return ServerResponse.createByErrorMessage("请检查邮箱或密码！");
        }
        // 检查用户是否被禁用
        if (loginUser.getUserStatus().equals(UserStatus.NOT_IN_THE_NET.getStatus())) {
            return ServerResponse.createByErrorMessage("您的账户已经被禁用，如有疑问请联系管理员!");
        }
        // 把用户存放在session中
        request.getSession(true).setAttribute(Constant.CURRENT_USER, loginUser);
        logger.info("user {} login success",user.getUserEmail());
        return ServerResponse.createBySuccess("登录成功！",CookieUtil.getValueByCookie(Constant.LOGIN_BACK_URL, request));
    }

}
